import json
import sys
from pathlib import Path


def process_json_file(file_path):
    """对指定JSON文件中的数组去重并排序"""
    try:
        with open(file_path, "r+", encoding="utf-8") as f:
            data = json.load(f)

            # 处理 redeemCodes
            if "redeemCodes" in data:
                data["redeemCodes"] = sorted(list(set(data["redeemCodes"])))

            # 处理 excludeCodes
            if "excludeCodes" in data:
                data["excludeCodes"] = sorted(list(set(data["excludeCodes"])))

            # 写回文件（格式化缩进2空格）
            f.seek(0)
            json.dump(data, f, indent=2, ensure_ascii=False)
            f.truncate()

    except Exception as e:
        print(f"处理文件 {file_path} 时出错: {str(e)}")
        return False
    return True


if __name__ == "__main__":
    json_files = [Path("redeemCode.json")]

    all_success = True
    for file in json_files:
        if not process_json_file(file):
            all_success = False

    sys.exit(0 if all_success else 1)
